using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Serialization;
using RedRiver.Framework.XtraCommon.Templates;

namespace RedRiver.Framework.XtraCommon.FormHelper{
    public partial class PropertiesSetting : DataViewForm{
        public PropertiesSetting(){
            InitializeComponent();
            //gridControl.DataSource = typeof (PropertiesCollection); // new Properties();
            //gridControl.DataMember = "Property";
            //colPropertyName.FieldName = "Key";
            //colPropertyValue.FieldName = "Value";
            //gridControl.DataSource = new PropertiesCollection();
        }

        private void typeSelectorButtonEdit1_EditValueChanged(object sender, EventArgs e){
            if (typeSelectorButtonEdit1.EditType != null){
                repositoryItemComboBox_propertyName.Items.Clear();
                PropertyInfo[] properties =
                    typeSelectorButtonEdit1.EditType.GetProperties(BindingFlags.Instance | BindingFlags.Public |
                                                                   BindingFlags.SetProperty);
                foreach (PropertyInfo property in properties){
                    if (property.PropertyType.IsValueType){
                        repositoryItemComboBox_propertyName.Items.Add(property.Name);
                    }
                }
                defaultBindingSource.AddNew();
            }
        }

        protected override void OnUpdateData(){
            base.OnUpdateData();
            new XmlSerializer(gridControl.DataSource.GetType()).Serialize(Console.Out, gridControl.DataSource);
        }
    }

    [Serializable]
    public class PropertiesCollection : CollectionBase{
        public KeyValuePair<string, string> this[int index]{
            get { return (KeyValuePair<string, string>) InnerList[index]; }
            set { this[index] = value; }
        }

        public int Add(KeyValuePair<string, string> pair){
            int index = Indexof(pair.Key);
            if (index > -1){
                this[index] = pair;
            } else{
                index = InnerList.Add(pair);
            }
            return index;
        }

        public int Add(string key, string value){
            int index = Indexof(key);
            if (index > -1){
                this[index] = new KeyValuePair<string, string>(key, value);
            } else{
                index = InnerList.Add(new KeyValuePair<string, string>(key, value));
            }
            return index;
        }

        public bool Contains(string key){
            foreach (KeyValuePair<string, string> pair in InnerList){
                if (pair.Key.Equals(key)){
                    return true;
                }
            }
            return false;
        }

        public int Indexof(string key){
            for (int i = 0; i < InnerList.Count; i++){
                if (this[i].Key.Equals(key)){
                    return i;
                }
            }
            return -1;
        }
    }
}